package com.hq.common.utils;

import java.util.Random;

public class StringUtil {

    private static final String code="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    private static final String nu="1234567890";

    //判断源字符串是否有值，空引号也算没值
    public static boolean hasText(String  string){
        return string!=null&&!"".equals(string);
    }

    //  "a b"
    // 判断源字符串是否有值，空引号和空格也算没值
    public static boolean hasTextAndSpace(String  string){
        return string!=null&&!"".equals(string.trim());
    }

    //判断是否为手机号码
    public static boolean isPhone(String  string){
//        boolean matches=false;
//       if(hasTextAndSpace(string)){
//           matches = string.matches("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9])|(19[0-9]))\\d{8}$");
//       }
//        return matches;
        return  hasTextAndSpace(string)&&string.matches("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9])|(19[0-9]))\\d{8}$");
    }

    //判断是否为电子邮箱
    public static boolean isEmail(String  string){
        return  hasTextAndSpace(string)&&string.matches("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
    }

    //判断是否全部为字母
    public static boolean isLetter(String  string){
        return  hasTextAndSpace(string)&&string.matches("^[a-zA-Z]+$");
    }

    //获取n位随机英文字符串
    //1:ASCII 码
    public static String  getLetter(int len){
        //【0,1)*52\
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < len; i++) {
            char c = code.charAt((int) (Math.random() * code.length()));
            sb.append(c);
        }
        return sb.toString();
    }


    //获取n位随机英文和数字字符串
    public static String  getLetterAndNum(int len){
        //【0,1)*52\
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < len; i++) {
            char c = (code+nu).charAt((int) (Math.random() * (code+nu).length()));
            sb.append(c);
        }
        return sb.toString();
    }


    //获取n个随机中文字符串
    public static String getCh(int length){
        int i1 = Integer.parseInt("9fa5", 16);
        int i2 = Integer.parseInt("4e00", 16);
       // 取 i2到i1的随机数
        // Math.random()*(最大值-最小值)+最小值   =》【最小值,最大值)
        //Math.random()*(最大值-最小值)+最小值+1   =》【最小值,最大值)
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < length; i++) {
            int num=(int)(Math.random()*(i1-i2)+i2+1);
            sb.append((char)num);
        }
        return sb.toString();
    }


}
